using Microsoft.AspNetCore.Mvc;
using MRMDS.Module;
using MRMDS.Module.Response;
using MRMDS.Utils;
using System;
using Path = MRMDS.Module.Path;

namespace Demo.Controllers
{

    [ApiController]
    [Route("Actor")]
    public class ActorRelateController : ControllerBase
    {
        private readonly GlobalManager global;
        private readonly ILogger<ActorRelateController> _logger;

        public ActorRelateController(ILogger<ActorRelateController> logger)
        {
            _logger = logger;
            global = GlobalManager.Instance;
        }

        [Route("Info")]
        [HttpPut]
        public DefaultResponse UpdateInfo([FromQuery] string uuid, [FromBody] ActorInfo info)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                Logger.Write($"Info Update Fail (0).(uuid:{uuid},name:{info.Name})");
                return DefaultResponse.FailUUIDNullRefer;
            }
            instance.Info = info;
            Logger.Write($"Info Update.(uuid:{uuid},name:{info.Name})");
            return DefaultResponse.SuccessNullMessage;
        }

        [Route("Info")]
        [HttpGet]
        public ActorInfo? GetInfo([FromQuery] string uuid)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                Logger.Write($"Info Get Fail (0).(uuid:{uuid})");
                return null;
            }
            return instance.Info;
        }

        [Route("State")]
        [HttpPut]
        public DefaultResponse UpdateState([FromQuery] string uuid, [FromBody] ActorState state)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                Logger.Write($"State Update Fail (0).(uuid:{uuid})");
                return DefaultResponse.FailUUIDNullRefer;
            }
            instance.State = state;
            return DefaultResponse.SuccessNullMessage;
        }

        [Route("State")]
        [HttpGet]
        public ActorState? GetState([FromQuery] string uuid)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                Logger.Write($"State Get Fail (0).(uuid:{uuid})");
                return null;
            }
            return instance.State;
        }

        [Route("Command")]
        [HttpPost]
        public DefaultResponse SetCommand([FromQuery] string uuid, [FromBody] Command command)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                Logger.Write($"Command Set Fail (0).(uuid:{uuid},Command:{command.Type})");
                return DefaultResponse.FailUUIDNullRefer;
            }
            instance.Command = command;
            Logger.Write($"Command Set Success.(uuid:{uuid},Command:{command.Type})");
            return DefaultResponse.SuccessNullMessage;
        }

        [Route("Command")]
        [HttpGet]
        public Command? GetCommand([FromQuery] string uuid)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                Logger.Write($"Command Get Fail (0).(uuid:{uuid})");
                return null;
            }
            return instance.Command;
        }

        [Route("Path")]
        [HttpPut]
        public DefaultResponse UpdatePath([FromQuery] string uuid, [FromBody] Path path)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                Logger.Write($"Path Update Fail (0).(uuid:{uuid})");
                return DefaultResponse.FailUUIDNullRefer;
            }
            instance.Path = path;
            return DefaultResponse.SuccessNullMessage;
        }

        [Route("Path")]
        [HttpGet]
        public Path? GetPath([FromQuery] string uuid)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                Logger.Write($"Path Get Fail (0).(uuid:{uuid})");
                return null;
            }
            return instance.Path;
        }

        [Route("Event")]
        [HttpPut]
        public DefaultResponse SendEvent([FromQuery] string uuid, [FromBody] string eventMessage)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                return DefaultResponse.FailUUIDNullRefer;
            }
            instance.Event = eventMessage;
            return DefaultResponse.SuccessNullMessage;
        }

        [Route("Event")]
        [HttpGet]
        public string? GetEvent([FromQuery] string uuid)
        {
            if (!global.UUID2Actor.TryGetValue(uuid, out Actor? instance))
            {
                return null;
            }
            return instance.Event;
        }

        [Route("Infos")]
        [HttpGet]
        public IEnumerable<ActorInfoResponse> GetAllInfos()
        {
            List<ActorInfoResponse> infoes = new List<ActorInfoResponse>();
            foreach (var pair in global.UUID2Actor)
            {
                if (!(pair.Value is Actor))
                {
                    continue;
                }
                infoes.Add(new ActorInfoResponse()
                {
                    UUID = pair.Key,
                    Info = (pair.Value as Actor)!.Info
                });
            }
            Logger.Write($"User Request FastShot.");
            return infoes;
        }
    }
}